#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""

import datetime
import hashlib
import time
import base64
import requests
import os
import re
import sys
import json
import pandas
import chardet
import graphene
import config
from common.view import BaseApi
from res.lijiacai.utils import tool


class phoneSearch(BaseApi, tool.RandomString):
    name = "phoneSearch"
    description = "电话查询"

    class Argument:
        phone0_2 = graphene.String(description="电话前3位", required=True)
        province = graphene.String(description="省份", required=True)
        city = graphene.String(description="市区", required=True)
        operators = graphene.List(graphene.String, description="运营商")
        phone7_10 = graphene.String(description="电话后4位", required=True)

    class Return:
        class phoneSearch1(graphene.ObjectType):
            province = graphene.String(description="省份")
            city = graphene.String(description="市区")
            operators = graphene.String(description="运营商")
            phone11 = graphene.String(description="手机号")

        count = graphene.Int(description="数量")
        results = graphene.List(phoneSearch1, description="列表")

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        phone0_2 = self.arguments.get("condition", {}).get("phone0_2")
        province = self.arguments.get("condition", {}).get("province")
        city = self.arguments.get("condition", {}).get("city")
        operators = self.arguments.get("condition", {}).get("operators", [])
        phone7_10 = self.arguments.get("condition", {}).get("phone7_10")
        url = "https://chahaoba.com/api/phone11_query.php?"
        data = {
            "phone0_2": phone0_2,
            "province": province,
            "city": city,
            "phone7_10": phone7_10
        }
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
            "Host": "chahaoba.com"
        }
        res = requests.post(url=url, data=data, headers=headers).json()
        rows = []
        for i in res.get("results", []):
            if i["operators"] in operators:
                rows.append(i)
        return {"count": len(rows), "results": rows}


class ProvinceCity(BaseApi, tool.RandomString):
    name = "ProvinceCity"
    description = "省份城市"

    class Argument:
        phone0_2 = graphene.String(description="电话前3位", required=True)

    class Return:
        class ProvinceCity1(graphene.ObjectType):
            province = graphene.String(description="省份")
            city = graphene.String(description="市区")

        count = graphene.Int(description="数量")
        results = graphene.List(ProvinceCity1, description="列表")

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        phone0_2 = self.arguments.get("condition", {}).get("phone0_2")
        url = "https://chahaoba.com/api/phone11_query.php?phone0_2={}".format(phone0_2)
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
            "Host": "chahaoba.com"
        }
        res = requests.get(url=url, headers=headers).json()
        return {"count": len(res), "results": res}
